// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./documentSign.css");
	var databind = require("databind");
	var media = require("media");
	var financeId;
	var financeState;
	var contractType;
	var inventoryId = [];
	var remark;
	var tools = require("tools");
	var caseSizeMap = new tools.Map();
	var storage = window.sessionStorage; 

	var init = function(page) {
		financeId = storage.getItem("financeId");
		financeState=page.params.financeState;
		contractType=2;
		if(financeId==null){
			financeId = page.params.financeId;
		}else{
			storage.removeItem("financeId");
		}
		if(financeState == 23 || financeState == 22){
			$(".pickupForm p").remove();
			$(".submit-document").remove();
			$(".documentSignTitle").remove();
		}
		
		inquireFinanceContract();
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
		// 点击签署合同
		$(".submit-document").unbind("click").click(function() {
			databind.submitDom("#documentSignList", function(data) {
				base.alert(1, "合同签署成功！");
				//router.route("account/accountDetail.html?financeId="+financeId);
				router.route("account/documentSign.html?financeId=" + financeId+"&financeState="+23);
			});
		});
		
		
	};
	
	var inquireFinanceSignatureInfo =function(fileUuid){
		base.fnAjax({
			action : "inquireFinanceContractInfo.json",
			async : false, // 同步异步
			oData : {
				fileUuid : fileUuid
			}, // 传递参数
			fnSuccess : function(data) {
				if (data.status == 200) {
					if (data.datas.length > 0 && data.datas[0].financeContractId != 0) {
						$('.media').media({
							  width: 800,
							  height: 800,
							  src: data.datas[0].platformFileUrl
						});
					} 

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	var inquireFinanceContract = function() {
		base.fnAjax({
			action : "inquireFinanceContract.json",
			async : false, // 同步异步
			oData : {
				financeId : financeId,
				contractType: contractType
			}, // 传递参数
			fnSuccess : function(data) {
				if (data.status == 200) {
					if (data.datas.length > 0 && data.datas[0].financeContractId != 0) {
						//$(".financeContract").attr("action", "updateFinanceContract");
						showImg(data.datas[0].materials);
						$("#materials").val(data.datas[0].materials);
						$("#documentSignList input[name=receive]").val(data.datas[0].receive);
						$("#documentSignList input[name=deliver]").val(data.datas[0].deliver);
						$("#documentSignList input[name=financeId]").val(data.datas[0].financeId);
						$("#documentSignList input[name=financeContractId]").val(data.datas[0].financeContractId);
						$("#documentSignList input[name=financeContractSerialNumber]").val(data.datas[0].financeContractSerialNumber);
						$("#documentSignBody").append('<input class="field-input" name="url"  value="'+window.location.host+'" type="hidden">');
					} 

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	// 显示pdf
	var showImg = function(materials) {
		var fileUuids = materials.split(",");
		var html = "";
		var imgSrc = "";
		for (var i = 0; i < fileUuids.length; i++) {
			imgSrc = fileUuids[i];
			if (imgSrc.length != 0) {
				inquireFinanceSignatureInfo(imgSrc);   //显示pdf
				//html +='合同附件:<br/><a target="_blank" class="mas" fileuuid="'+imgSrc+'" href="/finance/api/1.0/downloadFile.file?fileUuid=' + imgSrc + '">1.pdf</a>';
				//html += '合同附件:<br/><a  target="_blank"  href="/finance/api/1.0/downloadFile.file?fileUuid=' + imgSrc + '"><img  src="../../../res/png/file_icon.png" width="100" height="100" class="finance-field-img"  alt="' + imgSrc + '" /></a>';
				//$("#documentSignBody").append(html);
			}
		}
	}
     
	
	


	return {
		init: init
	};
});